package fr.uparis10.miage.m1p1.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import fr.uparis10.miage.classic.m1.p3.Departement;
import fr.uparis10.miage.classic.m1.p3.ObjectJ;


public class ObjectJReader extends PropertyReader {
	private static ObjectJReader theInstance = null;
	
	/**
	 * Private Constructor - singleton class
	 */
	private ObjectJReader() {
	}
	
	public static ObjectJReader getInstance() {
		if (theInstance == null) {
			theInstance = new ObjectJReader(); 
		}
		return theInstance;
	}
	
	/**
	 * Read & parse the ObjectJ file
	 * @param aFile
	 * @return
	 */
	public ObjectJ readObjectJ(File aFile) {
		Properties prop = new Properties();
		FileInputStream inStream;
		
		ObjectJ entreprise = Configurator.getInstance().getObjectJ();
		
				
		try {
			inStream = new FileInputStream(aFile);
			prop.load(inStream);
		} catch (FileNotFoundException exc) {
			log.log(Level.SEVERE, "Exception occured: ", exc);
			return entreprise;
		} catch (IOException exc) {
			log.log(Level.SEVERE, "Exception occured: ", exc);
			return entreprise;
		}
		String region;
		double ca;
		int numsave;
		String nomPdg;
		// debut de lecture de données departement
		
			/* Start to read & convert */
			ca = getDoubleProperty(prop,"object_chiffreAffaire",0.0d);
			numsave = getIntProperty(prop,"object_saveno",0);
			region = getStringProperty(prop, "object_region", "");
			nomPdg = getStringProperty(prop,"object_nompdg","");
			/* Create the Entreprise objectj */
			entreprise = new ObjectJ(ca, numsave, region,nomPdg);
			ArrayList<Departement> depart=new ArrayList<Departement>();
			depart= Configurator.getInstance().getDepartementListe();
			for(Departement d:depart)
				entreprise.addDepartement(d);
		return entreprise;

 

	}
}
